﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using KillBill.Common.Validation;


namespace KillBill.Presentation.Common.WindowsForms.AutoValidableControls
{
    public partial class AVTextBox : UserControl, IAutoValidableControl
    {
        private readonly DataDictionary _dataDictionary = new DataDictionary();
        private Image _imgError;
        private Image _imgOk;

        [Browsable(true)]
        public Image ImgError
        {
            get
            {
                if (_imgError == null)
                    _imgError = KillBill.Presentation.Common.WindowsForms.Properties.Resources.cancel;
                return _imgError;
            }
            set { _imgError = value; }
        }
        [Browsable(true)]
        public Image ImgOk
        {
            get
            {
                if (_imgOk == null)
                    _imgOk = KillBill.Presentation.Common.WindowsForms.Properties.Resources.accept;
                return _imgOk;
            }
            set { _imgOk = value; }
        }

        [Browsable(true)]
        [DefaultValue(false)]
        public bool Multiline
        {
            set
            {
                textBox1.Multiline = value;
            }
            get
            {
                return textBox1.Multiline;
            }
        }

        [DefaultValue(true)]
        [Browsable(true)]
        public bool AllowNull { get; set; }

        [DefaultValue(DataType.String)]
        [Browsable(true)]
        public DataType Type
        {
            get { return _dataDictionary.Type; }
            set { _dataDictionary.Type = value; }
        }
        public new string Text
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value;}
        }
        public AVTextBox()
        {
            InitializeComponent();
        }

        private void UserControl1_Resize(object sender, EventArgs e)
        {
            textBox1.Height = Size.Height;
            textBox1.Width = Size.Width - 22;
            pictureBox1.Location = new Point(Size.Width - 20, 2);
        }
        private void InitialValue()
        {
            pictureBox1.Image = IsValid ? ImgOk : ImgError;
            if (textBox1.Text.Length == 0)
                pictureBox1.Image = null;
        }

        public bool IsValid
        {
            get
            {
                if (DesignMode)
                    return true;
                if (AllowNull && textBox1.Text.Trim(' ').Length == 0)
                    return true;
                return _dataDictionary.ValidateLocalValue(textBox1.Text, Type);
            }
        }
        private void TextBoxAVimg_Load(object sender, EventArgs e)
        {
            InitialValue();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            InitialValue();
        }
    }
}
